/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.calendar;

import assisisw.smartcal.entitysubsystem.Calendar;
import assisisw.smartcal.entitysubsystem.ICalendarController;
import assisisw.smartcal.entitysubsystem.IUserController;
import assisisw.smartcal.entitysubsystem.User;
import assisisw.smartcal.entitysubsystem.exceptions.calendar.CalendarException;
import assisisw.smartcal.entitysubsystem.exceptions.user.UserNotRegisteredException;
import java.util.Collections;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author Francesco
 */
@ManagedBean ( eager = true )
@Named ( value = "editCalendarControllerBean" )
@ViewScoped ()
public class EditCalendarControllerBean extends AbstractCalendarControllerBean
{

    // Instance Methods
    
    /***/
    @EJB
    private IUserController userController ;
    
    /***/
    private boolean newSharedMailValid = false ;
        
    // Instance Methods
    
    /***/
    public EditCalendarControllerBean () {}
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void calendarViewConfirmationCommand () 
    {
        ICalendarController controller ; 
        Calendar calendar ;
        controller = getCalendarController();
        calendar = getManagedCalendar () ;
        calendar.setOwner(getDashboardBean().getManagedUser()); 
        controller.update(calendar); 
        getDashboardBean().onCalendarUpdated(calendar); 
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void calendarViewDeleteCommand () 
    {
        ICalendarController controller ;
        Calendar calendar ;
        calendar = getManagedCalendar() ;
        controller = getCalendarController();
        controller.delete(calendar); 
        getDashboardBean().onCalendarDeleted(calendar); 
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void calendarViewShareCommand () 
    {
        Calendar thisCalendar ;
        User selectedUser ;
        if ( userController.isRegistered ( getNewSharedMail() ) )
        {
            try 
            {
                thisCalendar = getManagedCalendar () ;
                selectedUser = userController.getUser( getNewSharedMail () ) ;
                if ( ! selectedUser.equals ( getDashboardBean().getManagedUser () ) )
                {
                    if ( ! thisCalendar.getPeopleWhoCanAccess().contains ( selectedUser ) )
                    {
                        try 
                        {
                            // he is ok, no mail for him
                            getCalendarController().share ( thisCalendar , Collections.singleton ( selectedUser ) ) ;                            
                            FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( "Calendar Correctly Shared !" ) ) ;
                        } 
                        catch (CalendarException ex) 
                        {
                        }
                    }
                    else
                    {
                        FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( "You have already shared this Calendar with him !" ) ) ;
                        // no double sharing allowed
                    }
                }
                else
                {
                    FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( "This Calendar is yours, you don't have to self-share it !" ) ) ;
                    // no self sharing allowed
                }
            } 
            catch (UserNotRegisteredException ex) 
            {
                // problems
            }
        }
        else
        {
            // for now
            FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( "Sorry, but this user is not in SmartCAL !" ) ) ;
            // not registered, we should send him an email and manage this
        }
    }
    
    @Override
    public void setManagedCalendar ( Calendar calendar ) 
    {
        super.setManagedCalendar(calendar); 
        if ( ! calendar.getOwner ().equals ( getDashboardBean ().getManagedUser () ) )
            setCalendarEditable(false);
        else
            setCalendarEditable(true);
        newSharedMailValid = false ;
    }
    
    public void setNewSharedMailValid ()
    {
        newSharedMailValid = true ;
    }
    
}
